<template>
  <view class="content pt-30 pb-50 pl-20 pr-20">
    <view class="detail-block pd-20">
      <view style="display: flex;">
        <view class="title-block">核查信息</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患名称</view>
        <view class="item-value">{{detail.htName ? detail.htName : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患等级</view>
        <view class="item-value">{{detail.levelName ? detail.levelName : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患描述</view>
        <view class="item-value">{{detail.description ? detail.description : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">检查类型</view>
        <view class="item-value">{{detail.checkType ? checkTypeJson[detail.checkType] : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">隐患地点</view>
        <view class="item-value">{{detail.place ? detail.place : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">检查单位</view>
        <view class="item-value">{{detail.checkDept ? detail.checkDept : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">检查人</view>
        <view class="item-value">{{detail.checkPeo ? detail.checkPeo : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">所属风险点</view>
        <view class="item-value">{{detail.riskPoint ? detail.riskPoint : ''}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">所属单位</view>
        <view class="item-value">{{detail.beCheckDept ? detail.beCheckDept : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改方式</view>
        <view class="item-value">{{detail.rectificationType == 1 ? '限期整改' : '立即整改'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改责任人</view>
        <view class="item-value">{{detail.rectificationPeo ? detail.rectificationPeo : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改责任单位</view>
        <view class="item-value">{{detail.rectificationDept ? detail.rectificationDept : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改期限</view>
        <view class="item-value">{{detail.rectificationLimit ? detail.rectificationLimit : '-'}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">剩余时间</view>
        <view class="item-value">{{getRemainingTime(nowDateTime, detail.rectificationLimit)}}</view>
      </view>
      <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">整改前图片</view>
        <view class="image-list">
          <image v-for="(item, index) in detail.ossImg" :key="index" class="image" mode="aspectFill" :src="item.url" @click="previewImage(item.url)"></image>
        </view>
      </view>
      <!-- 此处存在回显bug 原因参数名不一致 -->
     <!-- <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">延期审批单位</view>
       //  <view class="item-select" @click="() => deferApplyDeptIdState = true">
        <view class="item-select" @click="routeByUrl('/pagesCommon/departmentList?paramsKey=deferApprove')">
          <text v-if="params.deferApproveDeptId" class="mr-10">{{params.deferApproveDeptId}}</text>
          <text v-else class="item-default-text mr-10">请选择延期审批单位</text>
          <u-icon name="arrow-right" size="26rpx" color="#CCCCCC"></u-icon>
        </view>
      </view>-->
      <!-- <view class="item-block pt-20 pb-20 border-b-common">
        <view class="item-label">延期审批人</view>
        // <view class="item-select" @click="() => deferApplyPeoIdState = true">
        <view class="item-select" @click="routeByUrl('/pagesCommon/departmentList?paramsKey=deferApprove')">
          <text v-if="params.deferApprovePeoId" class="mr-10">{{params.deferApprovePeoId}}</text>
          <text v-else class="item-default-text mr-10">请选择延期审批人</text>
          <u-icon name="arrow-right" size="26rpx" color="#CCCCCC"></u-icon>
        </view>
      </view>-->
      <view class="item-block pt-20">
        <view class="item-label">延期时间</view>
        <view class="item-select" @click="() => calendarState = true">
          <text v-if="params.deferDate">{{params.deferDate}}</text>
          <text v-else class="item-default-text mr-10">请选择延期时间</text>
          <u-icon name="calendar" size="46rpx" color="#3F99F7"></u-icon>
        </view>
      </view>
    </view>
    <view class="detail-block pd-20 mt-30">
      <view class="mb-20" style="display: flex;">
        <view class="title-block">延期原因</view>
      </view>
      <view>
        <textarea class="text-area pd-20" placeholder="请输入延期原因" placeholder-class="placeholder" v-model="params.deferApplyInfo"></textarea>
      </view>
    </view>
    <view class="button-block mt-30">
      <u-button type="info" text="取消" :custom-style="{width: '340rpx', backgroundColor: '#FFFFFF !important'}" @click="routeBack()"></u-button>
      <u-button type="primary" text="确认" :custom-style="{width: '340rpx'}" @click="submit"></u-button>
    </view>
    <!-- <u-calendar :show="calendarState" mode="single" color="#3F99F7" :closeOnClickOverlay="true" @confirm="calendarConfirm" @close="calendarClose"></u-calendar> -->
    <u-picker :show="deferApplyDeptIdState" :columns="deferApplyDeptList" keyName="label" @confirm="pickerConfirm('deferApplyDeptId', $event)" @cancel="pickerCancel('deferApplyDeptId')"></u-picker>
    <u-picker :show="deferApplyPeoIdState" :columns="deferApplyPeoList" keyName="label" @confirm="pickerConfirm('deferApplyPeoId', $event)" @cancel="pickerCancel('deferApplyPeoId')"></u-picker>
    <u-datetime-picker :show="calendarState" v-model="deferDate" :closeOnClickOverlay="true" :minDate="minDate(detail.rectificationLimit)" mode="datetime" @confirm="calendarConfirm" @close="() => dateTimeState = false" @cancel="() => dateTimeState = false"></u-datetime-picker>
  </view>
</template>

<script>
  import { getDangerEditDetailApi, deferApplyDangerEditInfoApi } from '@/apis/danger/index.js'
  import { getDeptDictListApi, getUserDictListApi } from '@/apis/danger/common.js'
  import checkTypeJson from '@/static/json/checkType.json'
  import dayjs from 'dayjs'
  
  export default {
    data() {
      return {
        nowDateTime: dayjs().valueOf(),
        checkTypeJson,
        calendarState: false,
        params: {
          deferDate: '',
          deferApplyInfo: '',
          deferApplyPeoId: '',
          deferApprovePeoId: '',
          deferApplyDeptId: '',
          deferApproveDeptId: ''
        },
        deferDate: '',
        deferApplyPeoIdState: false,
        deferApplyPeoList: [],
        deferApplyDeptIdState: false,
        deferApplyDeptList: [],
        detail: {}
      }
    },
    onLoad(options) {
      console.log(options)
      this.getDeptDictList()
      if(options.id) this.getDangerEditDetail({id: options.id})
    },
    methods: {
      getDangerEditDetail: function(params) {
        getDangerEditDetailApi(params).then(res => {
          this.detail = res
          if(dayjs().valueOf() < dayjs(res.rectificationLimit).valueOf()) {
            this.params.deferDate = dayjs(res.rectificationLimit).format('YYYY-MM-DD HH:mm:ss')
            this.deferDate = dayjs(res.rectificationLimit).format('YYYY-MM-DD HH:mm:ss')
          } else {
            this.params.deferDate = dayjs().format('YYYY-MM-DD HH:mm:ss')
            this.deferDate = dayjs().format('YYYY-MM-DD HH:mm:ss')
          }
        }).catch(err => console.warn(err))
      },
      getDeptDictList: function() {
        getDeptDictListApi({parentId: 1002}).then(res => {
          if(res.length > 0) res.map(item => Object.assign(item, {label: item.name, value: item.id}))
          this.deferApplyDeptList = [res]
        }).catch(err => console.warn(err))
      },
      getUserDictList: function(params) {
        getUserDictListApi(params).then(res => {
          if(res.length > 0) res.map(item => Object.assign(item, {label: item.nickname, value: item.id}))
          this.deferApplyPeoList = [res]
        }).catch(err => console.warn(err))
      },
      submit: function() {
        if(!this.authFormData()) return false
        let params = JSON.parse(JSON.stringify(this.params))
        Object.assign(params, { id: this.detail.id, htId: this.detail.htId })
        deferApplyDangerEditInfoApi(params).then(res => {
          setTimeout(() => uni.navigateBack(), 500)
        }).catch(err => console.log(err))
      },
      getRemainingTime: function(startTime = dayjs().format('YYYY-MM-DD HH:mm:ss'), endTime = dayjs().format('YYYY-MM-DD HH:mm:ss')) {
        if(dayjs(startTime).valueOf() < dayjs(endTime).valueOf()){
          var dayaa = dayjs(endTime).valueOf() - dayjs(startTime).valueOf()
          var days = parseInt(dayaa / (1000 * 60 * 60 * 24));
          var hours = parseInt((dayaa % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
          return days + " 天 " + hours + " 小时 " ;
        }
        // return startTime && endTime ? dayjs(startTime).diff(endTime, 'hour') : 0
      },
      previewImage: function(url) {
        uni.previewImage({ urls: [url] })
      },
      minDate: function(dateTime) {
        return dayjs(dateTime).valueOf()
      },
      pickerConfirm: function(type, event) {
        if(type == 'deferApplyDeptId') {
          this.params.deferApplyPeoId = ''
          this.params.deferApprovePeoId = ''
          this.getUserDictList({ deptId: event.value[0].value })
        }
        this.params[type] = event.value[0].value
        this.params[`${type}Name`] = event.value[0].label
        this[`${type}State`] = false
      },
      pickerCancel: function(type) {
        this[`${type}State`] = false
      },
      calendarConfirm: function(event) {
        // this.params.deferDate = event[0]
        this.params.deferDate = dayjs(event.value).format('YYYY-MM-DD HH:mm:ss')
        this.calendarState = false
      },
      calendarClose: function() {
        this.calendarState = false
      },
      routeByUrl: function(url) {
        uni.navigateTo({ url })
      },
      routeBack: function() {
        uni.navigateBack()
      },
      authFormData: function() {
        let params = this.params
        // if(!params.deferApproveDeptId) {
        //   uni.showToast({ title: '请选择延期审批单位', icon: 'none' })
        //   return false
        // }
        // if(!params.deferApprovePeoId) {
        //   uni.showToast({ title: '请选择延期审批人', icon: 'none' })
        //   return false
        // }
        if(!params.deferApplyInfo) {
          uni.showToast({ title: '请输入延期原因', icon: 'none' })
          return false
        }
        return true
      }
    }
  }
</script>

<style>
  page {
    background-color: #F4F4F4;
  }
  .u-button {
    background-color: #3F99F7 !important;
  }
</style>

<style lang="scss" scoped>
  .content {
    .detail-block {
      border-radius: 6rpx;
      background-color: #FFFFFF;
      .title-block {
        font-size: 30rpx;
        line-height: 42rpx;
        color: #222222;
        font-weight: 500;
        background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0) 50%, #C5DBFF 50%, #C5DBFF);
      }
      .item-block {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .item-label {
          font-size: 28rpx;
          line-height: 40rpx;
          color: #333333;
        }
        .item-value {
          font-size: 28rpx;
          line-height: 40rpx;
          color: #999999;
          max-width: 500rpx;
        }
        .item-select {
          display: flex;
          align-items: center;
          font-size: 28rpx;
          line-height: 40rpx;
          color: #333333;
          .item-default-text {
            color: #999999;
          }
        }
        .image-list {
          max-width: 520rpx;
          display: flex;
          justify-content: flex-end;
          flex-direction: row-reverse;
          flex-wrap: wrap;
          .image {
            width: 156rpx;
            height: 120rpx;
            border-radius: 6rpx;
            background-color: #CCCCCC;
            margin-left: 20rpx;
            margin-bottom: 20rpx;
          }
          .image:nth-child(3n) {
            margin-left: 0;
          }
        }
      }
      .text-area {
        width: 630rpx; 
        height: 300rpx;
        font-size: 28rpx;
        line-height: 40rpx;
        color: #333333;
        border: 1rpx solid #EEEEEE;
        border-radius: 6rpx;
      }
      .placeholder {
        font-size: 28rpx;
        line-height: 40rpx;
        color: #999999;
      }
    }
    .button-block {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }
</style>
